
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    $(function () {

        var year_1=1;
        var year_2=2;
        var year_3=3;

        var activity_mid=$("#activity_mid_hidden").val();
        //获取当前页数
        var page_num=$(".page_control .li_num.selected").text();
        gettable(year_1,activity_mid,1);
        //bindDDL(activity_mid,year_1);

        //查询
        $("#searchid1").bind("click", function () {
            //alert("yudan");
            gettable(year_1,activity_mid,1);
        });
        $("#searchid2").bind("click", function () {
            gettable(year_2,activity_mid,1);

        });
        $("#searchid3").bind("click", function () {
            gettable(year_3,activity_mid,1);

        });

        var act_id;
        //编辑
        $(document).on("click",".editaa",function(){
            act_id=$(this).attr("act_id");
            var act_name=$(this).attr("act_name");
            $("#textName").val(act_name);

        });
        $("#btnUpdate").click(function(){
            var group_mid=$("#selectgroupedit option:selected").attr("data-id");
            var menu_mid=act_id;

            var year_id=$("#year_id_hidden").val();

            var params={};
            params.group_mid=group_mid;
            params.menu_mid=menu_mid;


            IO.post("/shapan/api/selectMenu",params,function(d){
                if(d.code=="0"){
                    $('#myModal').modal('hide');
                    var page_num=$(".page_control .li_num.selected").text();
                    gettable(year_id,activity_mid,page_num);
                }
                else {
                    $('#myModal').modal('hide');
                    alert(d.msg);
                }
            })

        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            // 获取已激活的标签页的名称
            var activeTab = $(e.target).text();
            // 获取前一个激活的标签页的名称
            if(activeTab=="第一年度"){
                $("#year_id_hidden").val("1");

                gettable(1,activity_mid,1);
                //bindDDL(activity_mid,1);
            }
            else if(activeTab=="第二年度"){
                $("#year_id_hidden").val("2");

                gettable(2,activity_mid,1);
            }
            else
            {
                $("#year_id_hidden").val("3");
                gettable(3,activity_mid,1);
            }

        });

    });

    function gettable(year_id,activity_mid,page_num){

        var grid;

        //每列数据列性
        var columns = [
            {
                title: '年份',
                dataIndex: 'pro_nianfen',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].pro_nianfen + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '产品名称',
                dataIndex: 'pro_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" +data[datarow].pro_name + "</span>";
                }
            },
            {
                title: '需求市场',
                dataIndex: 'market',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" +data[datarow].market + "</span>";

                }
            },
            {
                title: '订单号',
                dataIndex: 'order_id',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>"+data[datarow].order_id+"</span>";

                }
            },
            {
                title: '产品需求量',
                dataIndex: 'pro_needcount',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+data[datarow].pro_needcount+"</span>";
                }
            },
            {
                title: '客户类型',
                dataIndex: 'channel',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+data[datarow].channel+"</span>";
                }
            },
            {
                title: '付款期限',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+data[datarow].paymentterm+"</span>";
                }
            },
            {
                title: '订单归属',
                dataIndex: 'group_mid',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='javascript:(0)' class='editaa' act_name='"+data[datarow].order_id+"' act_id='"+data[datarow].mid+"' data-toggle='modal' data-target='#myModal'>"+"请选择订单"+"</a></span>";
                }
            },
            {
                title: '是否被选',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].group_mid==null){
                        return "<img src='/images/not.png'/>"+"<span style='color: red;'font-size: 18px;>"+"未被选"+"</span>";
                    }
                    else
                    {
                        return "<img src='/images/done.png'/>"+"<span style='color: green;font-size: 14px;'>"+"已被选"+"</span>";
                    }
                }
            }
        ];

        //数组初始化

        function dataInit(year_id,activity_mid,page_num) {
            grid = new Grid("/shapan/api/bidding", $("#table_my_toast"+year_id), $("#paging_my_toast"+year_id), page_num, 10, columns, get_input_param(year_id,activity_mid), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param(year_id,activity_mid) {

            var pro_name,market,channel;

            if(year_id==1){
                pro_name= $("#selectgroup1 option:selected").attr("data-id");
                market=$("#selectRole1 option:selected").attr("data-id");
                channel=$("#selectType1 option:selected").attr("data-id");
            }
            else  if(year_id==2){
                pro_name= $("#selectgroup2 option:selected").attr("data-id");
                market=$("#selectRole2 option:selected").attr("data-id");
                channel=$("#selectType2 option:selected").attr("data-id");
            }
            else
            {
                pro_name= $("#selectgroup3 option:selected").attr("data-id");
                market=$("#selectRole3 option:selected").attr("data-id");
                channel=$("#selectType3 option:selected").attr("data-id");
            }
            var param="pro_name=" + pro_name+"&market="+market+"&year_id="+year_id+"&activity_mid="+activity_mid+"&channel="+channel;
            return param;
        }

        dataInit(year_id,activity_mid,page_num);
    }

    //绑定下拉框
    function bindDDL(activity_mid,year_id){

        var params={};
        params.activity_mid=activity_mid;

        IO.post("/shapan/api/groupNames",params,function(d){

            if(year_id==1){
                var s1= $("select.selectClass");
                s1.empty();
                $("<option value='" + "unde" + "'>" + "请选择" + "</option>").appendTo(s1);
                for(var i=0;i<d.data.length;i++){
                    $("<option value='" + d.data[i].mid + "'>" + d.data[i].group_name + "</option>").appendTo(s1);

                }
            }
            else if(year_id==2){
                var s2= $("select.selectClass2");
                s2.empty();
                $("<option value='" + "unde" + "'>" + "请选择" + "</option>").appendTo(s2);
                for(var j=0;j<d.data.length;j++){
                    $("<option value='" + d.data[j].mid + "'>" + d.data[j].group_name + "</option>").appendTo(s2);

                }
            }
            else {
                var s3= $("select.selectClass3");
                s3.empty();
                $("<option value='" + "unde" + "'>" + "请选择" + "</option>").appendTo(s3);
                for(var k=0;k<d.data.length;k++){
                    $("<option value='" + d.data[k].mid + "'>" + d.data[k].group_name + "</option>").appendTo(s3);

                }
            }

        });
    }

});
